package com.shirley.pers.zhufengfmvi.fragments.discovery;


import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.shirley.pers.zhufengfmvi.AlbumsDetailActivity;
import com.shirley.pers.zhufengfmvi.Constants;
import com.shirley.pers.zhufengfmvi.R;
import com.shirley.pers.zhufengfmvi.adapters.RecommendAdapter;
import com.shirley.pers.zhufengfmvi.customViews.CarouselImagesView;
import com.shirley.pers.zhufengfmvi.entities.DiscoveryColumnsList;
import com.shirley.pers.zhufengfmvi.entities.DiscoveryRecommendItem;
import com.shirley.pers.zhufengfmvi.entities.HighQualityListenList;
import com.shirley.pers.zhufengfmvi.entities.RecommendAlbums;
import com.shirley.pers.zhufengfmvi.fragments.BaseFragment;
import com.shirley.pers.zhufengfmvi.tasks.DiscoveryRecommendTask;
import com.shirley.pers.zhufengfmvi.tasks.TaskCallback;
import com.shirley.pers.zhufengfmvi.tasks.TaskResult;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 发现，推荐部分
 */
public class DiscoveryRecommandFragment extends BaseFragment implements TaskCallback, View.OnClickListener {

    private RecommendAdapter mAdapter;
    private List<DiscoveryRecommendItem> mItems;

    private static final String TAG = "DRF";
    private ListView mListView;

    public DiscoveryRecommandFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mItems = new ArrayList<>();
        mAdapter = new RecommendAdapter(getActivity(), mItems);
        mAdapter.setOnClickListener(this);

        DiscoveryRecommendTask task = new DiscoveryRecommendTask(this);
        task.execute();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.discovery_recommand_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.i(TAG, "onViewCreated: <<====================");
        mListView = ((ListView) view.findViewById(R.id.discovery_recommend_list));
        //获取头部焦点图片
        CarouselImagesView carouselImagesView = new CarouselImagesView(getActivity());
        mListView.addHeaderView(carouselImagesView);

        mListView.setAdapter(mAdapter);
        mListView.setItemsCanFocus(true);
    }

    @Override
    public String getFragmentTitle() {
        return "推荐";
    }

    @Override
    public void onTaskFinished(TaskResult result) {
        if (result != null) {
            int state = result.state;
            if (state == 0) {
                Object data = result.data;
                if (data != null) {
                    if (data instanceof JSONObject) {
                        JSONObject jsonObject = (JSONObject) data;
                        Log.d(TAG, "onTaskFinished: " + jsonObject.toString());
                        try {
                            RecommendAlbums albums = new RecommendAlbums();
                            //获取小编推荐
                            JSONObject jo = jsonObject.getJSONObject("editorRecommendAlbums");
                            albums.parseJson(jo);
                            mItems.add(albums);

                            //获取精品听单
                            JSONObject jospecialColumn = jsonObject.getJSONObject("specialColumn");
                            HighQualityListenList highQualityListenList = new HighQualityListenList();
                            highQualityListenList.parseJson(jospecialColumn);
                            mItems.add(highQualityListenList);

                            //获取发现新奇
                            JSONObject joDiscoveryColumns = jsonObject.getJSONObject("discoveryColumns");
                            DiscoveryColumnsList discoveryColumnsList = new DiscoveryColumnsList();
                            discoveryColumnsList.parseJson(joDiscoveryColumns);
                            mItems.add(discoveryColumnsList);


                            //获取热门推荐,数据结构同小编推荐相同，复用其实体类
                            JSONObject joHot = jsonObject.getJSONObject("hotRecommends");
                            JSONArray joHotArray = joHot.getJSONArray("list");
                            for (int i = 0; i < joHotArray.length(); i++) {
                                JSONObject jsonO = joHotArray.getJSONObject(i);
                                albums = new RecommendAlbums();
                                albums.parseJson(jsonO);
                                mItems.add(albums);
                            }
                            //通知适配器更新数据
                            mAdapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            } else if (state == 8) {
                Snackbar.make(getView(), "网络异常", Snackbar.LENGTH_SHORT).show();
            } else if (state == 9) {
                Snackbar.make(getView(), "服务器数据错误", Snackbar.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //---发现新奇事件响应
            case R.id.item_discovery_columns_image1_recommend_discovery_list:
                break;
            case R.id.item_discovery_columns_image2_recommend_discovery_list:
                break;
            case R.id.item_discovery_columns_image3_recommend_discovery_list:
                break;
            case R.id.item_discovery_columns_image4_recommend_discovery_list:
                break;

            //---精品听单事件响应
            case R.id.item_special_columns_image_top:
                break;
            case R.id.item_special_columns_image_bottom:
                break;

            //---小编推荐/热门推荐事件响应
            case R.id.item_left_coverLarge_recommend_discovery_list:
                getAlbumTrackIdToIntent(v);
                break;
            case R.id.item_middle_coverLarge_recommend_discovery_list:
                getAlbumTrackIdToIntent(v);
                break;
            case R.id.item_right_coverLarge_recommend_discovery_list:
                getAlbumTrackIdToIntent(v);
                break;
        }
    }

    /**
     * 获取专辑id和曲目id pass to Another Activity
     * @param v
     */
    private void getAlbumTrackIdToIntent(View v) {
        Object oTag = v.getTag();
        if (oTag != null) {
            Map map = (Map) oTag;
            Long mAlbumId = (Long) map.get(Constants.EXTRA_ALBUM_ID);
            Long mTrackId = (Long) map.get(Constants.EXTRA_TRACK_ID);
            Log.i(TAG, v + "|| mAlbumId:" + mAlbumId + ",mTrackId:" + mTrackId);
            Intent intent = new Intent(getActivity(), AlbumsDetailActivity.class);
            intent.putExtra(Constants.EXTRA_ALBUM_ID,mAlbumId);
            intent.putExtra(Constants.EXTRA_TRACK_ID,mTrackId);
            startActivity(intent);
        }
    }
}
